/**
 * 
 */
package com.training1.core.modules.service;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletResponse;

import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.training1.core.common.entity.BOXUserEntity;
import com.training1.core.common.global.signIn.BOXLoginInfoUtil;
import com.training1.core.common.service.CrudService;
import com.training1.core.common.utils.DateUtils;
import com.training1.core.common.utils.excel.ExcelUtil;
import com.training1.core.entity.ContractAttachEntity;
import com.training1.core.entity.ContractBaseEntity;
import com.training1.core.entity.ContractCrudEntity;
import com.training1.core.entity.UnderCompanyEntity;
import com.training1.core.modules.dao.ContractDao;
import com.training1.core.modules.dao.UserDao;

/**
 * @author yunlei
 *
 */
@Service
@Transactional
public class ContractService extends CrudService<ContractDao,ContractBaseEntity>{
	
	@Resource
	private UserDao userDao;
	
	@Resource
	private ContractDao contractDao;
	
	/**
	 * 判断登录用户的权限
	 * @param pageId 页面id
	 * @param userId 登录用户的id
	 * @return	如果查询结果为null或者为0,则该用户没有该权限
	 * 		如果查询结果>=1则用户有该权限
	 */
	public Integer getUserRole(Integer pageId,Integer userId){
		Map<String,Object> map = new HashMap<>();
		map.put("pageId", pageId);
		map.put("userId", userId);
		return contractDao.getUserRole(map);
	}
	/**
	 * 根据种类查询
	 * 逾期合同,待一审合同
	 * 
	 * @return
	 */
	public Map<String,Object> findCategList(Integer id){
		Map<String, Object> resultMap = new HashMap<String, Object>();
		
		//应收
		resultMap.put("yshtNum", contractDao.findYshtNum(id));
		resultMap.put("yshtSum", contractDao.findYshtSum(id));
		
		//--------------------------	
		//逾期合同(条数/合同金额)
//		resultMap.put("yqhtNum", contractDao.findYqhtNum(id));
//		resultMap.put("yqhtSum", contractDao.findYqhtSum(id));
//		//一审
//		resultMap.put("dyshtNum", contractDao.findDyshtNum(id));
//		resultMap.put("dyshtSum", contractDao.findDyshtSum(id));
//		//二审
//		resultMap.put("deshtNum", contractDao.findDeshtNum(id));
//		resultMap.put("deshtSum", contractDao.findDeshtSum(id));
//		//待放款
//		resultMap.put("dfkhtNum", contractDao.findDfkhtNum(id));
//		resultMap.put("dfkhtSum", contractDao.findDfkhtSum(id));
		Integer[] arr = new Integer[]{10,40,30,60};
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("id", id);
		for (Integer i : arr) {//90：已结束 10：逾期中 20：执行中 30：待二审 40：待一审 50：录入中 60：待放款 70：已作废
			map.put("status", i);
			if(i==10){//逾期合同(条数/合同金额)
				resultMap.put("yqhtNum", contractDao.findHtNum(map));
				resultMap.put("yqhtSum", contractDao.findHtSum(map));
			}else if(i==40){//一审
				resultMap.put("dyshtNum", contractDao.findHtNum(map));
				resultMap.put("dyshtSum", contractDao.findHtSum(map));
			}else if(i==30){//二审
				resultMap.put("deshtNum", contractDao.findHtNum(map));
				resultMap.put("deshtSum", contractDao.findHtSum(map));
			}else if(i==60){//待放款
				resultMap.put("dfkhtNum", contractDao.findHtNum(map));
				resultMap.put("dfkhtSum", contractDao.findHtSum(map));
			}
		}
		return resultMap;
	}
	//下拉框
	public List<UnderCompanyEntity> findCompanyCombox(){
		return dao.findCompanyCombox();
	}
	public BOXUserEntity findUserId(){
		//获取session中存入的用户名
		String userName = BOXLoginInfoUtil.getLoginInfo().getLoginName();
		Map<String, Object> param = new HashMap<String, Object>();
		param.put("userName", userName);
		return userDao.findUserId(param);
	}
	
	/**
	 * 导出excel文件
	 * @param response
	 * @param param
	 */
	public void export(HttpServletResponse response,Map<String, Object> param) {
		List<ContractBaseEntity> cqList = dao.findList(param);//根据dao中的内容用方法
		//设置列标题
		List<String> excelHead = new ArrayList<String>();
		excelHead.add("合同编号");
		excelHead.add("用户姓名");
		excelHead.add("所属公司");
		excelHead.add("联系电话");
		excelHead.add("拟借金额");
		excelHead.add("申请日期");
		excelHead.add("服务期数");
		excelHead.add("合同状态");
		
		//设置内容
		List<List<String>> dataList = new ArrayList<List<String>>();
		for(ContractBaseEntity cq : cqList){
			List<String> data = new ArrayList<String>();
			data.add(cq.getId().toString());
			data.add(cq.getUserName());
			data.add(cq.getUnderCompany());
			data.add(cq.getCustomerPhone());
			data.add(cq.getApprovalAmount().toString());
			data.add(cq.getAppDateStr());
			data.add(cq.getApprovalCyc().toString());
			if("10".equals(cq.getStatus())){
				data.add("逾期中");
			}else if("20".equals(cq.getStatus())){
				data.add("执行中");
			}else if("30".equals(cq.getStatus())){
				data.add("待二审");
			}else if("40".equals(cq.getStatus())){
				data.add("待一审");
			}else if("50".equals(cq.getStatus())){
				data.add("录入中");
			}else if("60".equals(cq.getStatus())){
				data.add("待放款");
			}
			dataList.add(data);
		}
		
		//设置文件名
		String fileName;
		String sheetName = "sheet1";
		String titleName;
		fileName = "合同逾期表_"+DateUtils.getDate().replace("-", "");
		titleName = "逾期表_"+DateUtils.getDate().replace("-", "");
		
		ExcelUtil.exportExcel(response, excelHead, dataList, fileName, sheetName,titleName);
	}
	
	
	/**
	 * 导出excel文件
	 * @param response
	 * @param param
	 */
	public void exportList(HttpServletResponse response,Map<String, Object> param) {
		List<ContractBaseEntity> entity = dao.searchContractList(param);//根据dao中的内容用方法
		//设置列标题
		List<String> excelHead = new ArrayList<String>();
		excelHead.add("合同编号");
		excelHead.add("所属公司");
		excelHead.add("客户姓名");
		excelHead.add("客户手机");
		
		excelHead.add("产品金额");
		excelHead.add("销售日期");
		excelHead.add("服务期数");
		excelHead.add("合同状态");
		
		//设置内容
		List<List<String>> dataList = new ArrayList<List<String>>();
		for(ContractBaseEntity e : entity){
			List<String> data = new ArrayList<String>();
			data.add(e.getId().toString());
			
			data.add(e.getUnderCompany());
			data.add(e.getUserName());
			data.add(e.getCustomerPhone());
			data.add(e.getApprovalAmount().toString());
			data.add(e.getAppDateStr());
			data.add(e.getApprovalCyc().toString());
			if("10".equals(e.getStatus())){
				data.add("逾期中");
			}else if("20".equals(e.getStatus())){
				data.add("执行中");
			}else if("30".equals(e.getStatus())){
				data.add("待二审");
			}else if("40".equals(e.getStatus())){
				data.add("待一审");
			}else if("50".equals(e.getStatus())){
				data.add("录入中");
			}else if("60".equals(e.getStatus())){
				data.add("待放款");
			}else if("70".equals(e.getStatus())){
				data.add("已作废");
			}else if("90".equals(e.getStatus())){
				data.add("已结束");
			}
			dataList.add(data);
		}
		
		//设置文件名
		String fileName;
		String sheetName = "sheet1";
		String titleName;
		fileName = "合同查询表_"+DateUtils.getDate().replace("-", "");
		titleName = "合同查询表_"+DateUtils.getDate().replace("-", "");
		
		ExcelUtil.exportExcel(response, excelHead, dataList, fileName, sheetName,titleName);
	}
	//添加合同
	public String saveContract(ContractCrudEntity entity){
		Integer customerId = contractDao.saveCustomer(entity);//客户id.
		entity.setCustomerId(customerId);
		Integer contractId = contractDao.saveContract(entity);//hetongid
		entity.setContractId(contractId);
		
		ContractAttachEntity att = new ContractAttachEntity();
		att.setContractBaseid(contractId.toString());
		att.setFileType("1");
		att.setFileName("IDcardSFZPhoto");
		att.setFileAdd(entity.getPhotoOfIdCard());
		contractDao.saveFujian(att);
		String[] arr1 = entity.getLvyueContract();
		String[] spn = entity.getMinusSpn();
		for(int i=0;i<arr1.length;i++){
			ContractAttachEntity attach = new ContractAttachEntity();
			attach.setAttachName("附件"+spn[i]);
			attach.setFileAdd(arr1[i]);
			attach.setFileType("2");
			attach.setContractBaseid(contractId.toString());
			contractDao.saveFujian(attach);
		}
		String[] arr2 = entity.getQitaContract();
		String[] qtfjName = entity.getQtfjName();
		for(int i=0;i<arr2.length;i++){
			ContractAttachEntity attach = new ContractAttachEntity();
			attach.setAttachName(qtfjName[i]);
			attach.setFileAdd(arr2[i]);
			attach.setFileType("9");
			attach.setContractBaseid(contractId.toString());
			contractDao.saveFujian(attach);
		}
		return null;
	}
	
	//修改合同的保存方法
	public void updateSaveContract(ContractCrudEntity entity){
		dao.updateCustomer(entity);
		dao.updateContract(entity);
		//删除该合同的所有的附件
		dao.deletAllFujian(entity.getContractId());
		ContractAttachEntity att = new ContractAttachEntity();
		att.setContractBaseid(entity.getContractId().toString());
		att.setFileType("1");
		dao.updateContract(entity);
		att.setFileAdd(entity.getPhotoOfIdCard());
		contractDao.saveFujian(att);
		String[] arr1 = entity.getLvyueContract();
		String[] spn = entity.getMinusSpn();
		
		/*Integer[] fileIdArr = entity.getFileId();
		StringBuilder sb = new StringBuilder();
		sb.append("(");
		//所有剩下文件的id
		for(int i=0;i<fileIdArr.length;i++){
			sb.append(fileIdArr[i]);//把所有剩余的文件id拼接起来
			if(i!=fileIdArr.length-1)
			sb.append(",");
		}
		sb.append(")");
		
		int index = 1;//文件个数
		//把不在这里面的所有都删掉
		Map<String,Object> map = new HashMap<>();
		map.put("conId", entity.getContractId());//所属合同id
		map.put("ids", sb.toString());//文件id组成的字符串
		contractDao.deletFileContract(map);*/
		
		for(int i=0;i<arr1.length;i++){
			ContractAttachEntity attach = new ContractAttachEntity();
			//attach.setId(fileIdArr[index++]);//附件id
			attach.setAttachName("附件"+spn[i]);
			attach.setFileAdd(arr1[i]);
			attach.setFileType("2");
			attach.setContractBaseid(entity.getContractId().toString());
			contractDao.saveFujian(attach);
		}
		String[] arr2 = entity.getQitaContract();
		String[] qtfjName = entity.getQtfjName();
		for(int i=0;i<arr2.length;i++){
			ContractAttachEntity attach = new ContractAttachEntity();
			//attach.setId(fileIdArr[index++]);//附件id
			attach.setAttachName(qtfjName[i]);
			attach.setFileAdd(arr2[i]);
			attach.setFileType("9");
			attach.setContractBaseid(entity.getContractId().toString());
			contractDao.saveFujian(attach);
		}
	}
	
	public List<ContractCrudEntity> getContract(Integer id){
		return contractDao.getContract(id);
	}
	
	/**
	 * 查询的是所有合同列表
	 * 查询所有的合同
	 */
	public List<ContractBaseEntity> searchContractList(Map<String,Object> map){
		return dao.searchContractList(map);
	}
	//表格
	public Integer exist(Integer id){
		return dao.exist(id);
	}
	
	public void delContract(Integer id){
		dao.delContract(id);
	}
		//select count(*) from T_CONTRACT_DELAY where IS_VALID=0 and CONTRACTBASEID=1	
	
}
